package cn.k.controller;

import cn.k.domain.MessageBoard;
import cn.k.domain.MessageReply;
import cn.k.domain.SystemDictionary;
import cn.k.qo.QueryObject;
import cn.k.service.IMessageBoardService;
import cn.k.service.IMessageReplyService;
import cn.k.service.ISystemDictionaryService;
import cn.k.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/message")
public class MessageBoardController {

    @Autowired
    private IMessageBoardService messageBoardService;

    @Autowired
    private IMessageReplyService messageReplyService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    //处理分页查询页面请求
    @RequestMapping("/list")
    @RequirePermission(name = "留言板查询",expression = "messageBoard:list")
    public String list(QueryObject qo, Model model){
        PageInfo<MessageBoard> pageInfo = messageBoardService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        List<SystemDictionary> category= systemDictionaryService.queryByService("business");
        model.addAttribute("category",category);
        return "message/list";
    }


    //处理删除请求
    @RequirePermission(name = "留言板删除",expression = "messageBoard:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id!=null){
            messageBoardService.delete(id);
        }
        return "redirect:/message/list";
    }


    //处理添加或者更新的请求
    @RequirePermission(name ="留言板编辑",expression = "messageBoard:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(MessageBoard messageBoard){

        //判断有没有id
        if (messageBoard.getId()==null){
            messageBoardService.insert(messageBoard);
        }else {
            messageBoardService.update(messageBoard);
        }
        return "redirect:/message/list";
    }

    //处理添加或者更新的请求
    @RequirePermission(name ="留言板编辑",expression = "messageBoard:detail")
    @RequestMapping("/detail")
    public String detail(Long id,Model model){
        MessageBoard messageBoard = messageBoardService.get(id);
        model.addAttribute("messageBoard",messageBoard);

        //回复条数
        MessageBoard replyCount =messageBoardService.selectReplyCount(id);
        model.addAttribute("replyCount",replyCount);


        List<MessageReply> messageReplyList=messageReplyService.selectByMessageId(id);
        model.addAttribute("messageReplyList",messageReplyList);

        return "/message/detail";
    }
    


}
